﻿import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.io.*;



public class SetBodyData /*extends JFrame*/  implements ActionListener
{	
	public static final int WIDTH  = 200 ;				//畫面寬度
	public static final int HEIGHT = 200 ;				//畫面高度	
	JFrame f = new JFrame( "SetBodyData" ) ;
	private String  Name ;
	private String  Sex ;
	private String  Age ;
	private String  Height ;
	private String  Weight ;
	private double  BMI ;
	private Function Ft ;
	//UTF-8編碼測試
	JLabel l1 = new JLabel( "性別" ) ;
	JLabel l2 = new JLabel( "年齡" ) ;
	JLabel l3 = new JLabel( "身高" ) ;
	JLabel l4 = new JLabel( "體重" ) ;
	
	JTextField t1 = new JTextField( null , null , 10 ) ;
	JTextField t2 = new JTextField( null , null , 10 ) ;
	JTextField t3 = new JTextField( null , null , 10 ) ;
	JTextField t4 = new JTextField( null , null , 10 ) ;	
	
	JMenuBar  menuBar  = new JMenuBar( ) ;
	JMenu     sexMenu  = new JMenu( "boy" ) ;
	JMenuItem itemBoy  = new JMenuItem( "boy" ) ;
	JMenuItem itemGirl = new JMenuItem( "girl" ) ;
	
	@Override
	public void actionPerformed( ActionEvent e )
	{								
		f.setVisible( true ) ;	
	}	
	
	SetBodyData( String user )
	{

		this.Name = user ;
		//menuBar.setSize( new Dimension( 100, 12 ) ) ;
		//sexMenu.setSize( new Dimension( 100, 12 ) ) ;

		//menuBar.setBackground( Color.blue ) ;
		//sexMenu.setSize( 50 , 1 ) ;
		//sexMenu.setHorizontalTextPosition( SwingConstants.CENTER ) ;
		
		itemBoy.addActionListener( new sexMenu_act( ) ) ;
		sexMenu.add( itemBoy ) ;
		itemGirl.addActionListener( new sexMenu_act( ) ) ;
		sexMenu.add( itemGirl ) ;
		menuBar.add( sexMenu ) ;
		Ft = new Function( ) ;
		
		// 輸入身體基本資料的Jframe設定
		Container contentPane = f.getContentPane( ) ;
		contentPane.setLayout( new BorderLayout( ) ) ;
		
		JPanel p1  = new JPanel( ) ;
		p1.setLayout( new GridBagLayout( ) ) ;
		GridBagConstraints gbc = new GridBagConstraints( ) ;
		gbc.anchor = GridBagConstraints.WEST ;
		gbc.insets = new Insets( 3 , 3 , 3 , 3 ) ;
		
		p1.setBorder(BorderFactory.createTitledBorder( "輸入基本數據" ) ) ;

		// 送出資料的"確定"按鈕
		JButton KeyInData = new JButton( "確定" ) ;									
		KeyInData.addActionListener( new KeyInData_act( ) ) ;			
				
		// 排版
		gbc.gridy = 1;														
		gbc.gridx = 0;
		p1.add( l1 , gbc ) ; 
		gbc.gridx = 1 ;
		p1.add( menuBar , gbc ) ;
		
		gbc.gridy = 2 ;
		gbc.gridx = 0 ;
		p1.add( l2 , gbc ) ;
		gbc.gridx = 1 ;
		p1.add( t2 , gbc ) ;
		  
		gbc.gridy = 3 ;
		gbc.gridx = 0 ;
		p1.add( l3 , gbc ) ;
		gbc.gridx = 1 ;
		p1.add( t3 , gbc ) ;
		  
		gbc.gridy = 4 ;
		gbc.gridx = 0 ;
		p1.add( l4 , gbc ) ;	
		gbc.gridx = 1 ;
		p1.add( t4 , gbc ) ;
		  
		gbc.gridy = 5 ;
		gbc.gridx = 0 ;
		p1.add( KeyInData , gbc ) ;	
	     
		contentPane.add( p1 ) ;
	    f.pack( ) ;   
	}
	
	public void UserDataRemain( )
	{
		File saveFile = new File( Name + ".txt" ) ;
		try
		{
		    FileWriter fwriter = new FileWriter( saveFile ) ;
		    fwriter.write( Name + "\t" + Sex + "\t" + Age + "\t" + Height + "\t" + Weight ) ;
		    fwriter.close( ) ;
		}
		catch( Exception e )
		{
		    e.printStackTrace( ) ;
		}	
	}
		
	private class KeyInData_act implements ActionListener
	{
		@Override		
		public void actionPerformed(ActionEvent e) 
		{
			
			// 按"確定"後隱藏這個視窗
			f.setVisible(false);	
			// 得到從user輸入的資料
		    Sex    = sexMenu.getText( ) ;							
		    Age    = t2.getText( ) ;
		    Height = t3.getText( ) ;
		    Weight = t4.getText( ) ;
		    int IntTypeHeight = Integer.valueOf( Height ) ;
		    int IntTypeWeight = Integer.valueOf( Weight ) ;
		    BMI    = Ft.GetBMI( IntTypeWeight , IntTypeHeight ) ;
		    
		    JOptionPane.showMessageDialog( null , "BMI = " + BMI) ;
		    // 寫檔記錄
		    UserDataRemain( ) ;							
		}		
	}	
	
	private class sexMenu_act implements ActionListener
	{
		@Override		
		public void actionPerformed( ActionEvent ee ) 
		{
			JMenuItem a = (JMenuItem) ee.getSource( ) ;
			sexMenu.setText( a.getText( ) ) ;
		}		
	}	
	
	public String getUserBMI( )
	{
		String StringTypeBMI =  String.valueOf( BMI ) ;
		return StringTypeBMI ;
	}	
	
}
